﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Hk.Core.Extensions;

namespace Hk.Core.General
{
    public static class GenerateMeta
    {
        public static string AutoGenerateMeta(string page, string alias)
        {
            try
            {
                var title = "";
                var keyword = "";
                var description = "";
                var user = "";
                var publishedTime = "";
                var modifiedTime = "";
                var section = new List<string>();
                var images = new List<string>();
                var termServices = new Terms();
                var postServices = new Post();

                if (page.Equals("home") || page.Equals("search") || page.Equals("archives") && !string.IsNullOrEmpty(alias))
                {
                    title = alias;
                }
                else if (page.Equals("category") && !string.IsNullOrEmpty(alias))
                {
                    var term = termServices.GetByAlias(alias);
                    if (term != null)
                    {
                        title = term.name;
                    }
                }
                else if (page.Equals("detail") && !string.IsNullOrEmpty(alias))
                {
                    var pageDetail = postServices.GetByAlias(alias);
                    if (pageDetail != null)
                    {
                        title = pageDetail.MetaTitle;
                        keyword = pageDetail.MetaKeyword;
                        description = pageDetail.MetaDescription;

                        var cates = pageDetail.tbl_Blog_Term_Relationships;
                        foreach (var cate in cates)
                        {
                            section.Add(cate.tbl_Blog_Term_Taxonomy.tbl_Blog_Terms.name);
                        }
                        var imgs = pageDetail.tbl_Blog_Postmeta.Where(a => a.meta_key == PostMeta.image.ToString()).ToList();
                        if (imgs != null)
                        {
                            foreach (var img in imgs)
                            {
                                images.Add(img.meta_value);
                            }
                        }
                        user = pageDetail.tbl_Blog_Users.google_link;
                        publishedTime = pageDetail.post_date.ToString();
                        modifiedTime = pageDetail.post_modified.ToString();
                    }

                }
                return Meta(title, keyword, description, page, section, images, publishedTime, modifiedTime, user);
            }
            catch (Exception)
            {
                throw;
            }
        }

        private static tbl_Blog_Portal _option;
        private static tbl_Blog_Portal Option
        {
            get
            {
                if (_option == null)
                {
                    _option = new HkEntities().tbl_Blog_Portal.FirstOrDefault(a => a.Portal_Id == CurrentPortal.PortalId);
                }
                return _option;
            }
        }


        private static string Meta(string title, string keyword, string description, string page, List<string> section, List<string> images, string publishedTime, string modifiedTime, string user)
        {

            var strHtml = new StringBuilder();
            if (page.Equals("home"))
            {
                title += !string.IsNullOrEmpty(title) ? " | " + Option.blogname : Option.blogname;
                keyword += !string.IsNullOrEmpty(keyword) ? " | " + Option.blogkeywords : Option.blogkeywords;
                description += !string.IsNullOrEmpty(description) ? " | " + Option.blogdescription : Option.blogdescription;
            }
            else if (page.Equals("category") || page.Equals("archives") || page.Equals("search"))
            {
                title += " | " + Option.blogname;
                keyword += !string.IsNullOrEmpty(keyword) ? " | " + Option.blogkeywords : Option.blogkeywords;
                description += !string.IsNullOrEmpty(description) ? " | " + Option.blogdescription : Option.blogdescription;
            }
            else if (page.Equals("detail"))
            {
                if (!string.IsNullOrEmpty(publishedTime))
                    strHtml.AppendLine("    <meta property=\"article:published_time\" content=\"" + publishedTime + "\" />");
                if (!string.IsNullOrEmpty(modifiedTime))
                    strHtml.AppendLine("    <meta property=\"article:modified_time\" content=\"" + modifiedTime + "\" />");
                if (images.Count > 0)
                {
                    foreach (var img in images)
                    {
                        strHtml.AppendLine("    <meta property=\"og:image\" content=\"" + img + "\" />");
                        strHtml.AppendLine("    <link rel=\"image_src\" href=\"" + img + "\" />");
                    }
                }
                if (section.Count > 0)
                {
                    foreach (var sec in section)
                    {
                        strHtml.AppendLine("    <meta property=\"article:section\" content=\"" + sec + "\" />");
                    }
                }
                strHtml.AppendLine("    <meta property=\"og:type\" content=\"article\" />");
                if (!string.IsNullOrEmpty(user))
                    strHtml.AppendLine("    <link rel=\"publisher\" href=\"https://plus.google.com/" + user + "\" />");
                strHtml.AppendLine("    <meta name=\"DC.date\" scheme=\"DCTERMS.W3CDTF\" content=\"" + publishedTime + "\" />");
                strHtml.AppendLine("    <meta property=\"og:audio\" content=\"" + HttpContext.Current.Request.Url + "\"/>");
                strHtml.AppendLine("    <meta property=\"og:audio:title\" content=\"" + title + "\"/>");
                strHtml.AppendLine("    <meta property=\"og:audio:album\" content=\"" + title + "\"/>");
                strHtml.AppendLine("    <meta property=\"og:audio:type\" content=\"audio/vnd.facebook.bridge\"/>");
                strHtml.AppendLine("    <meta property=\"og:type\" content=\"music.album\"/>");
                strHtml.AppendLine("    <meta property=\"music:release_date\" content=\"" + publishedTime + "\">");
            }
            //render seo
            strHtml.AppendLine("    <title>" + title + "</title>");
            if (!string.IsNullOrEmpty(keyword))
                strHtml.AppendLine("    <meta name=\"keywords\" content=\"" + keyword + "\" />");
            if (!string.IsNullOrEmpty(description))
                strHtml.AppendLine("    <meta name=\"description\" content=\"" + description + "\" />");
            strHtml.AppendLine("    <link rel=\"canonical\" href=\"" + HttpContext.Current.Request.Url + "\" />");
            if (!string.IsNullOrEmpty(Option.blogname))
                strHtml.AppendLine("    <meta property=\"og:site_name\" content=\"" + Option.blogname + "\" />");
            //Google
            strHtml.AppendLine("    <meta property=\"og:title\" content=\"" + title + "\" />");
            strHtml.AppendLine("    <meta property=\"og:keywords\" content=\"" + keyword + "\" />");
            strHtml.AppendLine("    <meta property=\"og:description\" content=\"" + description + "\" />");
            strHtml.AppendLine("    <meta property=\"og:url\" content=\"" + HttpContext.Current.Request.Url + "\" />");
            // DC
            strHtml.AppendLine("    <meta name=\"DC.title\" content=\"" + title + "\" />");
            strHtml.AppendLine("    <meta name=\"DC.Description\" content=\"" + description + "\" />");
            strHtml.AppendLine("    <meta name=\"DC.creator\" content=\"" + HttpContext.Current.Request.Url + "\" />");
            strHtml.AppendLine("    <meta name=\"DC.subject\" content=\"" + Option.blogname + "\" />");
            strHtml.AppendLine("    <meta name=\"DC.publisher\" content=\"" + Option.blogurl + "\" />");
            strHtml.AppendLine("    <meta name=\"DC.type\" scheme=\"DCTERMS.DCMIType\" content=\"Text\" />");
            strHtml.AppendLine("    <meta name=\"DC.format\" content=\"text/html; charset=UTF-8\" />");
            strHtml.AppendLine("    <meta name=\"DC.identifier\" scheme=\"DCTERMS.URI\" content=\"http://" + Option.blogurl + "\" />");

            strHtml.AppendLine("    <meta name=\"news_keywords\" content=\"" + keyword + "\" />");
            strHtml.AppendLine("    <!-- " + Option.blogurl + " Version -->");
            strHtml.AppendLine("    <meta name=\"author\" content=\""+Option.blogurl+"\">");
            strHtml.AppendLine("    <meta name=\"generator\" content=\"" + Option.blogurl + " 1.0.24\" />");
            strHtml.AppendLine("    <meta name=\"copyright\" content=\"" + Option.blogurl + "\" />");
            strHtml.AppendLine("    <meta name=\"revisit-after\" content=\"1 days\" />");
            strHtml.AppendLine("    <meta name=\"google-site-verification\" content=\"" + Option.google_site_verification + "\" />");
            strHtml.AppendLine("    <link rel=\"Shortcut Icon\" href=\"" + Option.favicon + "\" type=\"image/x-icon\" />");
            strHtml.AppendLine("    <meta name=\"distribution\" content=\"Global\" />");
            strHtml.AppendLine("    <meta name=\"robots\" content=\"index,follow\" />");
            strHtml.AppendLine("    <meta property=\"og:locale\" content=\"" + Option.locale + "\" />");
            strHtml.AppendLine("    <meta property=\"fb:app_id\" content=\"" + Option.app_id + "\" />");
            strHtml.AppendLine("    <meta property=\"fb:admins\" content=\"" + Option.admins + "\" />");
            strHtml.AppendLine("    <link rel=\"apple-touch-icon-precomposed\" sizes=\"144x144\" href=\""+Option.apple_touch_icon_144x144+"\">");
            strHtml.AppendLine("    <link rel=\"apple-touch-icon-precomposed\" sizes=\"72x72\" href=\"" + Option.apple_touch_icon_72x72 + "\">");
            strHtml.AppendLine("    <link rel=\"apple-touch-icon-precomposed\" href=\"" + Option.apple_touch_icon_57x57 + "\">");

            if (page.Equals("category") || page.Equals("archives") || page.Equals("search") || page.Equals("home"))
            {
                strHtml.AppendLine("    <meta property=\"og:type\" content=\"website\" />");
                strHtml.AppendLine("    <meta http-equiv=\"REFRESH\" content=\"1800\" />");
                strHtml.AppendLine("    <meta property=\"og:image\" content=\"/themes/images/logo.png\" />");
                strHtml.AppendLine("    <link rel=\"image_src\" href=\"/themes/images/logo.png\" />");
                strHtml.AppendLine("    <link rel=\"publisher\" href=\"https://plus.google.com/" + Option.google + "\" />");
                strHtml.AppendLine("    <meta name=\"DC.date\" scheme=\"DCTERMS.W3CDTF\" content=\"2014-04-02 12:24:36\" />");
            }

            return strHtml.ToString();
        }
    }
}
